package com.alipay.app.groupmeal.standard.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.appcompat.widget.AppCompatTextView;

import com.alipay.app.groupmeal.standard.R;

/**
 * author: lixiaoyan
 * date: 12/13/21
 * 自定义自适应文字内容控件
 */
public class CustomFitViewTextView extends AppCompatTextView {
    //控件的宽
    private int mViewWidth, mViewHeight;
    //可以设置的最小文字
    private float mMinTextSize = 10;
    private float mMaxTextSize;
    private float newTextSize;

    public CustomFitViewTextView(Context context) {
        this(context, null);
    }

    public CustomFitViewTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomFitViewTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        setFocusable(false);
    }

    /**
     * 初始化自定义属性
     */
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.BSFitViewTextView);
        mMinTextSize = mTypedArray.getDimension(R.styleable.BSFitViewTextView_BSMinTextSize, mMinTextSize);
        mMaxTextSize = getTextSize();
        newTextSize = mMaxTextSize;
        mTypedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        TextPaint mTextPaint = getPaint();
        mTextPaint.setTextSize(newTextSize);
        //获取当前设置文字的大小
        float mSourceTextSize = getTextSize();
        //获取内容
        String mLoadContent = getText().toString().trim();
        //测量内容的长度
        float mSourceTextContentSize = mTextPaint.measureText(mLoadContent);
        if (mSourceTextContentSize > mViewWidth && mViewWidth > 0) {//文字内容超过了控件的宽度
            //计算文字长度与文字大小的比例
            float scale = mSourceTextContentSize / mSourceTextSize;
            //根据控件长度计算合适的文字大小
            newTextSize = mViewWidth / scale;
            //增加最小限制
            if (newTextSize < mMinTextSize) {
                newTextSize = mMinTextSize;
            }
            //重新设置文字尺寸
            setTextSize(TypedValue.COMPLEX_UNIT_PX, newTextSize);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //计算控件的宽度（如果设置了padding值，需要减去左右的padding值）
        mViewWidth = MeasureSpec.getSize(widthMeasureSpec);
        mViewHeight = MeasureSpec.getSize(heightMeasureSpec);
//        LogUtil.d("mViewWidth: " + mViewWidth);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        if (getText().length() == 0) {
            setHint("0.00");
        } else {
            setHint("");
            newTextSize = mMaxTextSize;
        }
    }

}